package com.xx.sg.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Value("${sg.profile}")
    private String profile;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // URL 访问路径 /files/** 映射到本地路径 D:/upload/
        registry.addResourceHandler("/profile/**")
                .addResourceLocations("file:" + profile);
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // 配置跨域
        registry.addMapping("/**")  // 允许所有路径跨域
                //.allowedOrigins("*")  // 允许的前端地址
                .allowedOriginPatterns("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE")  // 允许的 HTTP 方法
                .allowedHeaders("*")  // 允许的请求头
                .allowCredentials(true)  // 是否允许发送 cookie
                .maxAge(3600);  // 预检请求缓存的最大时间
    }
}
